package com.fling.abcde.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fling.abcde.databinding.FragmentBaseBinding;
import com.lib.okhttp.NetClient;
import com.lib.common.bean.BaseResp;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author FengLing
 * Created on 2017-05-15 12:08
 */
public abstract class BaseFragment<T extends ViewBinding> extends Fragment implements NetClient.IDataChangeListener {
    protected FragmentBaseBinding baseViewBinding;
    protected T thisViewBinding;

    private final static boolean IS_SHOW_NET_ERROR[] = {true};
    protected NetClient mHttpsManager = new NetClient(this);

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewBinding();
    }

    private void initViewBinding() {
        LayoutInflater inflater = getLayoutInflater();
        baseViewBinding = FragmentBaseBinding.inflate(inflater);

        Type superclass = getClass().getGenericSuperclass();
        if (superclass == null) {
            return;
        }
        if (!(superclass instanceof ParameterizedType)) {
            return;
        }
        //获得父类的泛型参数的实际类型
        Type[] actualTypeArguments = ((ParameterizedType) superclass).getActualTypeArguments();
        Class<?> clz = (Class<?>) actualTypeArguments[0];
        try {
            //获取inflate方法 传入相应的参数
            Method method = clz.getDeclaredMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
            method.setAccessible(true);
            //执行inflate方法
            thisViewBinding = (T) method.invoke(null, inflater, null, false);
        } catch (Throwable e) {
            e.printStackTrace();
        }

        if (thisViewBinding != null) {
            View view = thisViewBinding.getRoot();
            baseViewBinding.fragContent.addView(view);
        }
    }

    @Override
    final public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return baseViewBinding.getRoot();
    }

    @Override
    public void onStart() {

        IS_SHOW_NET_ERROR[0] = false;
        super.onStart();
    }

    protected void showLoading() {
        baseViewBinding.fragLoadingLayer.setVisibility(View.VISIBLE);
    }

    protected void showLoading(String tip) {
        baseViewBinding.fragLoadingLayer.setVisibility(View.VISIBLE);
        baseViewBinding.fragLoadingTip.setText(tip);
    }

    protected void hideLoading() {
        baseViewBinding.fragLoadingLayer.setVisibility(View.GONE);
    }

    @Override
    public void onDataChanged(BaseResp baseResp) {

        if (baseResp.getCode() == 333) {
            hideLoading();
            BaseActivity activity = (BaseActivity) getActivity();
            if (activity != null) {
                activity.showOtherLogin();
            }
        }
    }

    @Override
    public void onProgressChanged(Integer progress) {

    }

    @Override
    public void onDestroy() {
        thisViewBinding = null;
        baseViewBinding = null;

        mHttpsManager.onDestroy();
        super.onDestroy();
    }
}